package com.jsz.peini.service.seller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jsz.peini.bean.seller.SellerDiscount;
import com.jsz.peini.bean.seller.SellerDiscountExample;
import com.jsz.peini.bean.seller.SellerDiscountExample.Criteria;
import com.jsz.peini.bean.seller.SellerInfo;
import com.jsz.peini.bean.seller.SellerJudge;
import com.jsz.peini.bean.seller.SellerStopapply;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.DateTimeUtils;
import com.jsz.peini.mapper.seller.SellerDiscountMapper;
import com.jsz.peini.mapper.seller.SellerInfoMapper;
import com.jsz.peini.mapper.seller.SellerStopapplyMapper;
import com.jsz.peini.mapper.task.TaskInfoMapper;

@Service
public class SellerInfoService {
	private static final Logger logger = LoggerFactory.getLogger(SellerInfoService.class);
	@Resource
	private SellerInfoMapper sellerInfoMapper;
	@Resource
	private SellerDiscountMapper sellerDiscountMapper;
	@Autowired
	private TaskInfoMapper taskInfoMapper;
	@Autowired
	private SellerStopapplyMapper sellerStopapplyMapper;
	
	public SellerInfo selectById(int id) {
		SellerInfo sellerInfo = sellerInfoMapper.selectById(id);
		return sellerInfo;
	}
	
	public List<LinkedHashMap<String, Object>> selectBySellerInfo(SellerInfo sellInfo){
		List<LinkedHashMap<String,Object>> sellerList=sellerInfoMapper.selectBySellerInfo(sellInfo);
		return sellerList;
	}
	
	public List<SellerInfo> selectBySearch(SellerInfo sellerInfo) {
		List<SellerInfo> sellerList = sellerInfoMapper.selectBySearch(sellerInfo);
		return sellerList;
	}

	public List<LinkedHashMap<String,Object>> getSellerCodesBySellerCodes(){
		List<LinkedHashMap<String,Object>> systemSeller=sellerInfoMapper.getSellerCodesBySellerCodes();
		return systemSeller;
	}
	
	public List<SellerInfo> selectListBySearch(SellerInfo sellerInfo) {
		List<SellerInfo> sellerList = sellerInfoMapper.selectListBySearch(sellerInfo);
		return sellerList;
	}
	
	public int updateByJudge(SellerJudge sellerJudge){
		int result = sellerInfoMapper.updateByJudge(sellerJudge);
		return result;
	}
	
	public LinkedHashMap<String, Object> updateByStatus(SellerInfo sellerInfo){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		infoMap.put("resultCode", Constants.SUCCESS);
		infoMap.put("resultDesc", "成功");
		infoMap.put("result",0);
		if (sellerInfo.getStatus()==0) {
			int result = sellerInfoMapper.updateByStatus(sellerInfo);
			if (result>0) {
			}
		}else if(sellerInfo.getStatus()==1){
			Integer tasknum=taskInfoMapper.getTaskInfoNumBySellerId(sellerInfo.getId());
			Integer smsnum=sellerStopapplyMapper.getCountNumBySellerId(sellerInfo.getId());
			if (tasknum>0) {
				infoMap.put("result",1);
				return infoMap;
			}else if (smsnum>0) {
				infoMap.put("result",2);
				return infoMap;
			}else {
				SellerStopapply sellerStopapply=new SellerStopapply();
				sellerStopapply.setSellerId((long)sellerInfo.getId());
				sellerStopapply.setApplyCode(sellerInfo.getAccname());
				sellerStopapply.setApplyType(1);
				sellerStopapply.setApplyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				sellerStopapply.setApplyStatus(0);
				sellerStopapplyMapper.insertSelective(sellerStopapply);
				return infoMap;
			}
		}
		return infoMap;
	}
	
	public List<HashMap<String,Object>> getSellerCoupons(Integer sellerId){
		List<HashMap<String,Object>> sellerMap = sellerInfoMapper.getSellerCoupons(sellerId);
		return sellerMap;
	}
	
	public SellerDiscount getSellerDiscount(SellerDiscount sellerDiscount){
		//SellerDiscount sellerMap = sellerInfoMapper.getSellerDiscount(sellerDiscount);
		SellerDiscount sellerMap = new SellerDiscount();
		sellerMap.setGoldNum(100);
		sellerMap.setWeixinNum(100);
		sellerMap.setZfbNum(100);
		try {
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			String dateStr = sdf.format(d);
			
			int week = DateTimeUtils.getWeek();
			SellerDiscountExample example = new SellerDiscountExample();
			Criteria criteria = example.createCriteria();
			criteria.andStartNumLessThanOrEqualTo(week);
			criteria.andEndNumGreaterThanOrEqualTo(week);
			criteria.andSellerInfoIdEqualTo(sellerDiscount.getSellerInfoId());
			List<SellerDiscount> list = sellerDiscountMapper.selectByExample(example);
			if(list.size() > 0){
				for (SellerDiscount dis : list) {
					String frontStr = DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd");
					String startTime = frontStr+" "+dis.getStartTime();
					String endTime = frontStr+" "+dis.getEndTime();
					
					//营业时间推后一小时
					Calendar cc = Calendar.getInstance();
					cc.setTime(DateTimeUtils.formatString2Date(endTime, "yyyy-MM-dd HH:mm"));
					cc.set(Calendar.HOUR_OF_DAY, cc.get(Calendar.HOUR_OF_DAY));
					endTime = DateTimeUtils.formatDateTime2LongString(cc.getTime());
					Date now = new Date();
					String nowStr = DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd HH:mm");
					if(DateTimeUtils.formatString2Date(startTime, "yyyy-MM-dd HH:mm").before(now) && DateTimeUtils.formatString2Date(endTime, "yyyy-MM-dd HH:mm").after(now)){
						sellerMap.setGoldNum(dis.getGoldNum());
						sellerMap.setWeixinNum(dis.getWeixinNum());
						sellerMap.setZfbNum(dis.getZfbNum());
						return sellerMap;
					}else if(startTime.equals(nowStr)){
						sellerMap.setGoldNum(dis.getGoldNum());
						sellerMap.setWeixinNum(dis.getWeixinNum());
						sellerMap.setZfbNum(dis.getZfbNum());
						return sellerMap;
					}else{
						Date start = DateTimeUtils.formatString2Date(startTime, "yyyy-MM-dd HH:mm");
						Date end = DateTimeUtils.formatString2Date(endTime, "yyyy-MM-dd HH:mm");
						if(end.before(start)){
							Date startCenter = DateTimeUtils.formatString2Date(DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd")+" 23:59:59", "yyyy-MM-dd HH:mm:ss");
							Calendar calendar = Calendar.getInstance();
							calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加1  
							Date endCenter = DateTimeUtils.formatString2Date(DateTimeUtils.formatDateTime2String(calendar.getTime(), "yyyy-MM-dd")+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
							
							if(DateTimeUtils.formatString2Date(startTime, "yyyy-MM-dd HH:mm").before(now) && startCenter.after(now)){
								sellerMap.setGoldNum(dis.getGoldNum());
								sellerMap.setWeixinNum(dis.getWeixinNum());
								sellerMap.setZfbNum(dis.getZfbNum());
								return sellerMap;
							}else if(endCenter.before(endCenter) && DateTimeUtils.formatString2Date(endTime, "yyyy-MM-dd HH:mm").after(now)){
								sellerMap.setGoldNum(dis.getGoldNum());
								sellerMap.setWeixinNum(dis.getWeixinNum());
								sellerMap.setZfbNum(dis.getZfbNum());
								return sellerMap;
							}else if((DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd")+" 23:59").equals(nowStr) || (DateTimeUtils.formatDateTime2String(calendar.getTime(), "yyyy-MM-dd")+" 00:00").equals(nowStr)){
								sellerMap.setGoldNum(dis.getGoldNum());
								sellerMap.setWeixinNum(dis.getWeixinNum());
								sellerMap.setZfbNum(dis.getZfbNum());
								return sellerMap;
							}
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return sellerMap;
	}
	
	public List<HashMap<String,Object>> getSellerImages(int sellerId){
		List<HashMap<String,Object>> imageList = sellerInfoMapper.getSellerImages(sellerId);
		return imageList;
	}
}
